package com.bank.client;

import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@RequestMapping("/helloClient")
public class HelloClient {
    @Autowired
    public LoadBalancerClient loadBalancerClient;

    protected Logger logger = LoggerFactory.getLogger(HelloClient.class);
    @RequestMapping(value = "/clientHello",method = {RequestMethod.GET,RequestMethod.POST})
    public String clientHello(String name){
        ServiceInstance serviceInstance = loadBalancerClient.choose("nacos-discovery-server");
        String url = serviceInstance.getUri() + "/server/helloServer/serverHello?name=" + "lujie";
        logger.info("invoked url = " + url);
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.getForObject(url, String.class);
        return "Invoke : " + url + ", return : " + result;
    }

}
